package com.ljsw.update.version;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.ljsw.activity.R;

public class Loading {

	public Loading(Context context) {
		super();
		this.context = context;
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();// 获取屏幕分辨率
	}

	private Context context;
	private Dialog dialog;
	private Display display;
	Handler h;
	Bundle bun = null;
	ProgressBar bar;
	TextView percentText;
	private View v;
	private LinearLayout lLayout_bg;

	public Loading builder() {
		// 获取Dialog布局
		if (v == null) {
			v = LayoutInflater.from(context).inflate(R.layout.loading, null);
		}
		bar = (ProgressBar) v.findViewById(R.id.progress_version);
		percentText = (TextView) v.findViewById(R.id.percentText);
		lLayout_bg = (LinearLayout) v.findViewById(R.id.loading_lay);
		// 添加Dialog样式
		dialog = new Dialog(context, R.style.DeleteDialog);
		dialog.setContentView(v);
		// 调整dialog背景大小
		lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
				.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));
		return this;
	}

	/**
	 * 
	 * @param context
	 * @param h
	 *            安装通知
	 */
	public void loading(Context context, final Handler h_load) {
		try {
			h = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);

					switch (msg.what) {
					case 1:
						bun = msg.getData();
						double percent = bun.getDouble("bar");
						String p = null;
						if (percent <= 0) {
							p = "0.00";
						}

						try {
							if (percent >= 100) {
								p = "100";
							} else {
								p = (percent + "").substring(0,
										(percent + "").indexOf(".") + 3);
							}

						} catch (Exception e) {
							System.out.println(e.getMessage());
						}

						Log.i("percent", percent + "");
						percentText.setText(p + "%");
						int progressValue = (int) percent;
						Log.i("progressValue", progressValue + "");
						bar.setProgress(progressValue);
						break;

					case 100:
						// 下载完成后，通知安装
						Message ms = Message.obtain();
						ms.what = 100;
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						h_load.sendMessage(ms);
						dialog.dismiss();
						break;

					}

				}
			};
			dialog.show();

			// 下载新版本
			new Thread(new Run(h, context)).start();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void dismiss(){
		dialog.dismiss();
	}

	// 下载APK
	class Run implements Runnable {
		private Handler h;
		Context context;

		public Run() {
		}

		public Run(Handler h, Context context) {
			this.h = h;
			this.context = context;
		}

		public void run() {
			Online on = new Online(this.h);
			try {
				on.load(context);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
	}
}
